﻿import com.dmotiko.chabeli.*
import com.dmotiko.general.*
import flash.filters.BlurFilter;
import mx.transitions.easing.None;
import mx.transitions.easing.Regular;
import mx.transitions.Tween;
import mx.utils.Delegate;

class ChabeliProductList
extends BaseClip {
	
	private var container:MovieClip;
	private var nCount:Number;
	private var aPhotos:Array;
	private var nSpaceX:Number;
	private var nVelocity:Number;
			
	private function ChabeliProductList() {
		super();
	}
	
	public function onResize():Void {
		nSpaceX = Stage.width;
	}
	
	public function initClip():Void {
		Stage.addListener(this);
		onResize();
		nCount = 0;
		aPhotos = new Array();
		nVelocity = 0;
	}
		
	private function refreshData() {
		
		this.container = this.createEmptyMovieClip("container", 1);
		var nX:Number = 0;
		var nY:Number = 0;
		var nSpace:Number = 2;
		
		for (var i:Number = 0; i < data.length; i++) {
			var photoData:ChabeliProductData = ChabeliProductData(data[i]);
			var clip = this.container.attachMovie("productThumb", "photo" + i, i);
			clip._x = nX;
			clip._y = nY;
			nX += clip._width + nSpace;
			aPhotos.push( ChabeliProductLoader(clip) );
		}
		loadNextPhoto();
	}
	
	private function loadNextPhoto() {
		var photo:ChabeliProductLoader = aPhotos[nCount];
		if (!photo && nVelocity != 0 ) {
			moveBandas();
			return;
		}
		photo.addEventListener( MovieLoaderClip.CLIP_LOADED, Delegate.create(this, loadNextPhoto) );
		photo.addEventListener( BaseClipEvent.CHANGED, Delegate.create(this, photoSelected) );
		photo.setData( data[nCount] );
		nCount++;
	}
	
	private function moveBandas():Void {
		
		var nCenter:Number = nSpaceX /2;
		var root:ChabeliProductList = this;
		var bLocked:Boolean = false;
		
		this.onMouseMove = function() {
			bLocked = _ymouse > 0 && _ymouse < this._height;
		}
		
		for (var i:Number = 0; i < aPhotos.length; i++) {
			
			var clip = aPhotos[i];
			clip.partner = findPartner(clip);
			if (i == 0 ) {
				clip.guide = true;
			}
			clip.onEnterFrame = function() {
				if ( this._x < -this._width + 50 ) {
					this.guide = false;
					root.findNextGuide(this).guide = true;
					this._x = this.partner._x + this.partner._width + 2;
					//root.registerTween( this._name + "Fade", new Tween( this, "_alpha", Regular.easeOut, this._alpha, 0, 0.3, true), false, true);
				}
				if( this.guide) {
					if ( bLocked) this._x -= 3;
					else this._x -= root.nVelocity;
				} else {
					this._x = this.partner._x + this.partner._width + 2;
				}
			}
		}
	}
	private function findPartner( clip ):MovieClip {
		for (var i:Number = 0; i < aPhotos.length; i++) {
			var mc = aPhotos[i];
			if ( mc == clip ) {
				var partner = aPhotos[ i + 1 ] ? aPhotos[ i + 1 ] : aPhotos[ 0 ];
				return partner;
			}
			
		}
	}
	private function findNextGuide( clip ):MovieClip {
		for (var i:Number = 0; i < aPhotos.length; i++) {
			var mc = aPhotos[i];
			if ( mc == clip ) {
				var partner = aPhotos[ i - 1 ] ? aPhotos[ i - 1 ] : aPhotos[ aPhotos.length - 1 ];
				return partner;
			}
			
		}
	}
			
	private function photoSelected(evnt) {
		if(!evnt.loaded){
			var nIndex:Number;
			for (var i:Number = 0; i < aPhotos.length; i++) {
				if (aPhotos[i] == evnt.target) nIndex = i;				
			}
			var pSelected:ChabeliProductLoader = ChabeliProductLoader(evnt.target);
			dispatchEvent( { target: this, type: BaseClipEvent.CHANGED, index: nIndex } );
		}
	}
	
	private function tweenFinished( key:String, tween:Tween):Void {
		if ( key.indexOf( "Fade" ) > -1 ) {
			tween.yoyo();
		}
	}
	
	public function getVelocity():Number { return nVelocity; }
	
	public function setVelocity(value:Number):Void {
		nVelocity = value;
	}
	
	
}